package com.dingding.service.activity.core.dao.impl;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.StoreAssistant;
import com.dingding.service.activity.core.dao.IStoreAssistantDao;

/**
 * 
 * @ClassName: IUserCoupon 
 * @Description: 用户优惠券DAO接口
 * @author CHEHAUNBO 
 * @date 2016年2月3日 下午3:26:50
 * 
 */
@Component("storeAssistantDao")
public class StoreAssistantDaoImpl extends BaseDaoImpl<StoreAssistant> implements IStoreAssistantDao {

	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * @Title: validateUserPwd
	 * @Description: 验证手机号密码是否正确
	 * @author fengshuonan
	 * @return StoreAssistant
	 */
	@Transactional(readOnly = true)
	public StoreAssistant validateUserPwd(String tel, String pwd) {

		StoreAssistant assistant = null;

		try {
			assistant = super.queryCriteriaUnResult(
					Restrictions.and(Restrictions.eq("account", tel), Restrictions.eq("password", pwd)));
		} catch (Exception e) {
			log.error("验证门店用户登录密码时出错！StoreAssistantImpl:validateUserPwd()", e);
		}

		log.info("查询到店员！" + assistant);

		return assistant;
	}

	/**
	 * @Title: changeStoreUserPwd 
	 * @Description: 修改门店管理员密码
	 * @param tel 用户登录账户(手机号)
	 * @param pwd 验证码
	 * @author YHQ
	 * @return boolean    返回类型 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean changeStoreUserPwd(String tel, String pwd) {

		// 创建用于存储门店管理员信息对象
		StoreAssistant storeAssistant = null;

		// 设置获取门店管理员信息的条件
		Criterion criterion = Restrictions.eq("account", tel);

		try {
			// 根据设置条件获取门店管理员信息
			storeAssistant = super.queryCriteriaUnResult(criterion);
		} catch (Exception e) {
			log.error("修改门店管理员密码，获取门店管理员信息时出现异常  类：" + this.getClass() + " changeStoreUserPwd()", e);
			return false;
		}

		// 判断获取门店管理员信息是否为空，如果为空，用户不存在，修改失败
		if (storeAssistant == null) {
			return false;
		}

		// 修改门店管理员登录密码
		storeAssistant.setPassword(pwd);

		return true;
	}
}
